<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 创建父子关系 =>  3.使用 -->
    <!-- 输入框 -->
    <TodoHeader :list="list" @add="handelAdd"></TodoHeader>
    <!-- 列表区域 -->
    <!-- 父传子  1.父传     子传父  2.父收-->
    <TodoMain :list="list" @del="handelDel"></TodoMain>

    <!-- 统计和清空 -->
    <TodoFooter :list="list" @clear="list = []"></TodoFooter>
  </section>
</template>

<script>
//创建父子关系 =>  1.导入
import TodoHeader from './components/TodoHeader.vue'
import TodoMain from './components/TodoMain.vue'
import TodoFooter from './components/TodoFooter.vue'

export default {
  //创建父子关系 =>   2.注册
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  data () {
    return {
      list: JSON.parse(localStorage.getItem('newList')) || [
        { id: 1, name: '吃饭' },
        { id: 3, name: '自习' },
        { id: 4, name: '睡觉' }
      ]
    }
  },
  methods: {
    handelDel (id) {
      console.log(id)
      this.list = this.list.filter(item => item.id !== id)
      // this.list.splice('id', 1)
    },
    handelAdd (text) {
      this.list.unshift({
        id: +new Date(),
        name: text
      })
    }
  },
  watch: {
    list: {
      handler (val) {
        console.log(val)
        localStorage.setItem('newList', JSON.stringify(val))
      }
    }
  }
}
</script>

<style></style>
